package com.hqyj.zss.controller;

import com.hqyj.zss.pojo.Admin;
import com.hqyj.zss.pojo.PageData;
import com.hqyj.zss.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    //返回add页面
    @RequestMapping("/add")
    public String addOne(){
        return "add";
    }

    //返回addEmployee页面
    @RequestMapping("/addEmployee")
    public String addEmployee(){
        return "addEmployee";
    }

    //返回addAdmin页面
    @RequestMapping("/addAdmin")
    public String addAdmin(){
        return "add";
    }

    //查询全部admin信息
    @RequestMapping("queryAllAdmin")
    public String queryAllAdmin(ModelMap modelMap, Integer pageNumber, Integer pageSize){
        if(pageNumber == null){
            pageNumber = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }
        PageData pageData = adminService.queryAllAdmin(pageNumber,pageSize);
        modelMap.put("pageData", pageData);
        return "adminList";
    }

    //添加一个管理员
    @ResponseBody  // 含义: 方法返回的数据,直接放在响应体中
    @RequestMapping("/addOneAdmin")
    public String addOneAdmin(Admin admin,HttpServletRequest request){
        admin.setA_name(request.getParameter("a_name"));
        admin.setA_sex(request.getParameter("a_sex"));
        admin.setA_username(request.getParameter("a_username"));
        admin.setA_password(request.getParameter("a_password"));
        int i = adminService.addAdmin(admin);
        if(i>0){
            return "success";
        }
        return "fail";
    }

    /**
     * 登录控制
     * @param username
     * @param password
     * @return 返回请求参数，或者用户信息
     */
    @RequestMapping("/login")
    @ResponseBody
    public Map<String,Object> login(String username, String password){
        //检查请求参数
        if (username == null || password == null){
            Map<String,Object> result = new HashMap<>();
            result.put("code",-9);
            result.put("message","请求参数错误");
            return  result;
        }
        //调用服务层
        return adminService.adminLogin(username,password);
    }

    /**
     * 登出控制
     * @return
     */
    @RequestMapping("/logout")

    public String logout(){
        adminService.adminLogout();
        return "redirect: /OrderSystem_war_exploded/index.jsp";
    }

}
